package io.adrainty.bolee.framework.redis.resolver;

import org.springframework.core.convert.ConversionService;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheWriter;

import javax.annotation.Nonnull;

/**
 * <p>FrameworkRedisCache</p>
 *
 * @author AdRainty
 * @version V1.0.0
 * @description FrameworkRedisCache
 * @since 2025/8/1 15:34:18
 */
public class FrameworkRedisCache extends RedisCache {

    protected FrameworkRedisCache(String name,
                                  RedisCacheWriter cacheWriter,
                                  RedisCacheConfiguration cacheConfiguration) {
        super(name, cacheWriter, cacheConfiguration);
    }

    @Override
    public void evict(@Nonnull Object key) {
        if (key instanceof String keyStr && (keyStr.endsWith("*"))) {
                ConversionService conversionService = getCacheConfiguration().getConversionService();
                byte[] pattern = conversionService.convert(this.createCacheKey(key), byte[].class);
                if (pattern != null) {
                    this.getCacheWriter().clean(this.getName(), pattern);
                }

        }

        super.evict(key);
    }
}
